<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Administrador extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('User_model');
		$this->_is_logued_in();
		$this->load->helper("form");
		$this->load->library("form_validation");
		$this->load->library("email");
		$this->load->library("fpdf");
		$this->load->library("FusionCharts");
	}
	
	function index(){	
		$data['title']="CIPOST :: Administrador";
		$data['activo'] = "administrador";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['main_content']="administrador/indice";
		$this->load->view('includes/template',$data);
	}
	
	function mantenedores(){
		$data['title']="CIPOST :: Mantenedores";
		$data['activo'] = "administrador";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['main_content']="mantenedores/indice";
		$this->load->view('includes/template',$data);
	}
	
	function reportar_alumnos(){
		$data['title']="CIPOST :: Reportes Alumnos";
		$data['activo'] = "administrador";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$ing = $this->User_model->count_all_ingles('INGG01012012');
		$data['intTotalAnio1']=$ing;
		//$data['main_content']="administrador/ejemplo_grafico";
		//$this->load->view('administrador/ejemplo_grafico',$data);
		redirect('complements/ejemplografico/ejemplo_grafico',$data);
	}
	
	function listar_user(){
		$data['title']="CIPOST :: Administrador";
		$data['activo'] = "administrador";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['main_content']="administrador/listarusuarios";
		$data['query'] = $this->User_model->listar_user();
		$this->load->view('includes/template',$data);
	}
	
	function listar_alumnos(){
		$data['title']="CIPOST :: Administrador";
		$data['activo'] = "administrador";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['main_content']="administrador/listar";
		$data['query'] = $this->User_model->listar_alumnos();
		$data['us']="alu";
		$this->load->view('includes/template',$data);
	}
	
	function listar_matriculados(){
		$tipomatricula= $this->User_model->llenar_combo_tipomatricula();
		$sede= $this->User_model->llenar_combo_sede();
		$programa= $this->User_model->llenar_combo_programa();
		$cronograma= $this->User_model->llenar_combo_cronograma();
		$data = array('rs_tipomatricula' => $tipomatricula,'rs_sede' => $sede,'rs_programa' => $programa,'rs_cronograma'=>$cronograma);
		$data['title']="CIPOST :: Administrador";
		$data['activo'] = "administrador";
		$data['query'] = $this->User_model->listar_alumnos();
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['main_content']="administrador/listarmatriculados";
		$this->load->view('includes/template',$data);

	}
	
	function listar_alumnosmatr(){
		$tipomatricula= $this->input->post('tipomatricula');
		$sede= $this->input->post('sede');
		$programa= $this->input->post('programa');
		$cronograma= $this->input->post('cronograma');
		$cod_curso=$this->_generar_codigocurso($programa,$cronograma,$sede);
		if($this->User_model->curso_check($cod_curso)){
			$this->listar_matriculados();
		}
		else {
			$query=$this->User_model->listar_matriculados1($cod_curso,$tipomatricula);
		$this->listar_pdf($query);
}
	}
	
	
	function listar_pdf($query){
		

	//Create new pdf file
	$pdf=new FPDF();

	$pdf->AddPage();
	$pdf->AliasNbPages();
	$pdf->SetFillColor(232,232,232);
	$pdf->SetFont('Arial','B',12);
	$pdf->SetY(10);
	$pdf->Cell(190,6,'LISTADO DE ALUMNOS MATRICULADOS',1,0,'C',1);
	$i=10;
	$pdf->SetY(20);

	$pdf->Cell(20,6,'CODIGO',1,0,'L',1);
	$pdf->Cell(60,6,'NOMBRES',1,0,'L',1);
	$pdf->Cell(30,6,'DNI',1,0,'L',1);
	$pdf->Cell(80,6,'E-mail',1,0,'L',1);
		$i=10;
		$pdf->SetFont('Arial','',12);
		foreach($query as $row){
			$i=$i+5;
			$pdf->SetY(10+$i);
			$pdf->SetX(10);
			$pdf->Cell(20,6,$row->ALUMNO_cod_alumno,1,0,'L',1);
			$pdf->Cell(60,6,$row->apellidos.' '.$row->nombres,1,0,'L',1);
			$pdf->Cell(30,6,$row->dni,1,0,'L',1);
			$pdf->Cell(80,6,$row->email,1,0,'L',1);
		}
		$pdf->Output();
	}
	
	function _generar_codigocurso($programa,$cronograma,$sede){
		$codigo='';
		switch($programa){
		   	case 1:
		    	$codigo.='ALE';
		    	break;
			case 2:
		    	$codigo.='FRA';
		    	break;
			case 3:
		    	$codigo.='ING';
		    	break;
			case 4:
		    	$codigo.='ITA';
		    	break;			
		   default:
		      	$codigo.='POR';
		    	break;	
		}
		switch($cronograma){
		   	case 1:
		    	$codigo.='G01';
		    	break;
			case 2:
		    	$codigo.='G02';
		    	break;
			case 3:
		    	$codigo.='G03';
		    	break;
			case 4:
		    	$codigo.='G04';
		    	break;			
		   default:
		      	$codigo.='G05';
		    	break;	
		} 
		$codigo.='0';
		$codigo.=$sede;
		$codigo.=(date('Y')+1);
		return $codigo;
	}	
	
	function listar_pdf1(){
		$query = $this->User_model->listar_alumnos();

	//Create new pdf file
	$pdf=new FPDF();

	$pdf->AddPage();
	$pdf->AliasNbPages();
	$pdf->SetFillColor(232,232,232);
	$pdf->SetFont('Arial','B',12);
	$pdf->SetY(10);
	$pdf->Cell(190,6,'LISTADO DE ALUMNOS',1,0,'C',1);
	$i=10;
	$pdf->SetY(20);

	$pdf->Cell(20,6,'CODIGO',1,0,'L',1);
	$pdf->Cell(60,6,'NOMBRES',1,0,'L',1);
	$pdf->Cell(30,6,'DNI',1,0,'L',1);
	$pdf->Cell(80,6,'E-mail',1,0,'L',1);
		$i=10;
		$pdf->SetFont('Arial','',12);
		foreach($query as $row){
			$i=$i+5;
			$pdf->SetY(10+$i);
			$pdf->SetX(10);
			$pdf->Cell(20,6,$row->USUARIO_cod_usuario,1,0,'L',1);
			$pdf->Cell(60,6,$row->apellidos.' '.$row->nombres,1,0,'L',1);
			$pdf->Cell(30,6,$row->dni,1,0,'L',1);
			$pdf->Cell(80,6,$row->email,1,0,'L',1);
		}
		$pdf->Output();
	}
	
	function listar_docentes(){
		$data['title']="CIPOST :: Administrador";
		$data['activo'] = "administrador";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['main_content']="administrador/listar";
		$data['us']="doc";
		$data['query'] = $this->User_model->listar_docentes();
		$this->load->view('includes/template',$data);
	}

	function listar_pdf2(){
		$query = $this->User_model->listar_docentes();
	//Create new pdf file
	$pdf=new FPDF();

	$pdf->AddPage();
	$pdf->AliasNbPages();
	$pdf->SetFillColor(232,232,232);
	$pdf->SetFont('Arial','B',12);
	$pdf->SetY(10);
	$pdf->Cell(190,6,'LISTADO DE DOCENTES',1,0,'C',1);
	$i=10;
	$pdf->SetY(20);

	$pdf->Cell(20,6,'CODIGO',1,0,'L',1);
	$pdf->Cell(60,6,'NOMBRES',1,0,'L',1);
	$pdf->Cell(30,6,'DNI',1,0,'L',1);
	$pdf->Cell(80,6,'E-mail',1,0,'L',1);
		$i=10;
		$pdf->SetFont('Arial','',12);
		foreach($query as $row){
			$i=$i+5;
			$pdf->SetY(10+$i);
			$pdf->SetX(10);
			$pdf->Cell(20,6,$row->USUARIO_cod_usuario,1,0,'L',1);
			$pdf->Cell(60,6,$row->apellidos.' '.$row->nombres,1,0,'L',1);
			$pdf->Cell(30,6,$row->dni,1,0,'L',1);
			$pdf->Cell(80,6,$row->email,1,0,'L',1);
		}
		$pdf->Output();
	}


	function register_docente(){
		$data['title']="CIPOST :: Administrador";
		$data['activo'] = "administrador";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['main_content']="administrador/registrardocente";
		$this->load->view('includes/template',$data);
	}
	
	function register_alumnos(){
		$data['title']="CIPOST :: Administrador";
		$data['activo'] = "administrador";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['main_content']="administrador/registraralumnos";
		$this->load->view('includes/template',$data);
	}

function create_account(){
		$this->form_validation->set_rules('username','Nombre de Usuario','required|trim|min_length[4]|callback__username_check');
		$this->form_validation->set_rules('email','E-mail','required|trim|valid_email|callback__email_check');
		$this->form_validation->set_rules('password', 'Contraseña', 'required|md5');
		$this->form_validation->set_rules('repassword', 'Confirmar Contraseña', 'required|matches[password]|md5');
		$this->form_validation->set_message('required','El campo %s es requerido');
		$this->form_validation->set_message('valid_email','El campo %s no es válido');
		$this->form_validation->set_message('min_length','El campo %s debe tener mínimo 4 caracteres');
		$this->form_validation->set_message('_username_check','El %s ya existe.');
		$this->form_validation->set_message('_email_check','El %s ya se encuentra registrado.');
		$this->form_validation->set_message('matches','Las constraseñas no coinciden.');
		if ($this->form_validation->run() == FALSE)
		{
			$this->register_docente();
		}
		else
		{
			$username=$this->input->post('username');
			$password=$this->input->post('password');
			$email=$this->input->post('email');
			$activation_code=$this->_random_string(10);
			$nombres=$this->input->post('nombres');
			$apellidos=$this->input->post('apellidos');
			$dni=$this->input->post('dni');
			$direccion=$this->input->post('direccion');
			$telefono=$this->input->post('telefono');
			$insert=$this->User_model->register_docente($username,$password,$activation_code,$email,$nombres,$apellidos,$dni,$direccion,$telefono);	
		/*Email Confirmacion*/
		$config['protocol'] = "smtp";
		$config['smtp_host'] = "smtp.gmail.com";
		$config['smtp_port'] =587;
		$config['smtp_user'] = "cesserip@gmail.com";
		$config['smtp_pass'] = "xxxxxx";
		
		$config['mailtype'] = "html";
		$this->email->initialize($config);

		$this->email->from("soporte@cipost.com","CIPOST");
		$this->email->to($email);
		$this->email->subject("Verificacion de Registro CIPOST");
		$this->email->message("Por Favor de verificar su registro".anchor("http://localhost:8080/cipost/user/register_confirm/".$activation_code,"Confirmar Registro"));
		$this->email->send();
		echo "Porfavor confirme su registro en el siguiente link" . anchor("http://localhost:8080/cipost/user/register_confirm/" . $activation_code,"Confirmar Registro");
		
		}
	}
	
	function register_confirm($activation_code=''){
		$activation_code = $this->uri->segment(3);
		if($activation_code=='')
			die("C&oacute;digo de verificaci&oacute;n errado.");
		else{
			$update=$this->User_model->confirm_registration($activation_code);
			if($update){
				$data['title']= "CIPOST :: Inicio";
				$data['activo'] = "inicio";
				$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
				$data["mensaje"] = "Registro completo.";
				$data['main_content']="inicio/index";
	            $this->load->view('includes/template',$data);
			}
			else{
				$data['title']= "CIPOST :: Inicio";
				$data['activo'] = "inicio";
				$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
				$data["mensaje"] = "Su verificaci&oacute;n de Registro Fall&oacute;.";
				$data['main_content']="inicio/index";
	            $this->load->view('includes/template',$data);
			}
		}
	}
	
	function _username_check($username){
		return $this->User_model->username_check($username);
	}
		
	function _email_check($email){
		return $this->User_model->email_check($email);
	}
	
	function _random_string($length){
		$len = $length;
		$base='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789';
		$max=strlen($base)-1;
		$activatecode = '';
		mt_srand((double)microtime()*1000000);
		while(strlen($activatecode) < $len)
		{
			$activatecode .= $base{mt_rand(0,$max)};
		}
		return $activatecode;	
	}

	function _is_logued_in(){
		$is_logued_in=$this->session->userdata('is_logued_in');
		$user_id=$this->session->userdata('id');
		if($is_logued_in!=TRUE or $user_id='')		
			redirect('/index.php/user/login');
	}
}